<template>
  <!-- card -->
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <h2>筛选搜索</h2>
      <!-- Form表单 -->
      <el-form
        :inline="true"
        :model="queryParams"
        ref="queryForm"
        class="demo-form-inline"
      >
        <el-form-item label="输入搜索" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="角色名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loadRoles" :disabled="isLoading"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!-- button按钮 -->
    <el-button @click="handleDlg(0)">添加角色</el-button>
    <!-- table表格 -->
    <el-table
      :data="queryResult.records"
      v-loading="isLoading"
      border
      style="width: 100%"
    >
      <el-table-column type="index" label="序号" width="180" align="center">
      </el-table-column>
      <el-table-column prop="name" label="角色名称" width="180" align="center">
      </el-table-column>
      <el-table-column prop="description" label="描述" align="center">
      </el-table-column>
      <el-table-column
        prop="createdTime"
        label="添加时间"
        :formatter="timeFormatter"
        align="center"
      >
      </el-table-column>
      <el-table-column label="操纵" width="150px" align="center">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            @click="
              $router.push({
                name: 'alloc-menu',
                params: { roleId: scope.row.id },
              })
            "
            >分配菜单</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="
              $router.push({
                name: 'alloc-resource',
                params: { roleId: scope.row.id },
              })
            "
            >分配资源</el-button
          >
          <el-button size="mini" type="text" @click="handleDlg(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleRoleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- pagination分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryParams.current"
      :page-sizes="[3, 6, 10]"
      :page-size="3"
      layout="total, sizes, prev, pager, next, jumper"
      :disabled="isLoading"
      :total="queryResult.total"
    >
    </el-pagination>
    <!-- 使用子组件 -->
    <role-create-or-edit ref="roleDlg" @role-change="loadRoles" />
  </el-card>
</template>

<script>
import RoleCreateOrEdit from "./create-or-edit.vue";
import { queryRoles, deleteRole } from "@/api/role";
import dayjs from "dayjs";
export default {
  name: "RoleIndex",
  data() {
    return {
      //查询条件
      queryParams: {
        code: undefined,
        id: undefined,
        name: "",
        current: 1,
        size: 3,
      },
      //查询结果
      queryResult: {},
      //loading状态
      isLoading: false,
    };
  },
  created() {
    this.loadRoles();
  },
  methods: {
    //获取角色接口内容
    loadRoles() {
      this.isLoading = true;
      queryRoles(this.queryParams)
        .then((res) => {
          // console.log("角色列表", res);
          if (res.data.code === "000000") {
            const { current, pages, records, size, total } = res.data.data;
            Object.assign(this.queryParams, { current, size });
            this.queryResult = { pages, records, total };
            console.log(records);
          } else {
            this.$message.error("加载角色列表失败~");
          }
        })
        .finally(() => {
          this.isLoading = false;
        });
    },
    //时间格式化
    timeFormatter(row, column, value, index) {
      return dayjs(value).format("YYYY-MM-DD");
    },
    //重置
    handleReset() {
      this.$refs.queryForm.resetFields();
    },
    //查询
    //每页多少条
    handleSizeChange(val) {
      this.queryParams.size = val;
      //从第一页开始
      this.queryParams.current = 1;
      //重新查一遍
      this.loadRoles();
    },
    //点击当前页
    handleCurrentChange(val) {
      //当前页
      this.queryParams.current = val;
      this.loadRoles();
    },
    //呼出隐藏对话框
    handleDlg(roleId) {
      this.$refs.roleDlg.showAndInit(roleId);
    },
    //删除指定id的角色方法
    handleRoleDelete(roleId) {
      this.$confirm("确认要删除这个角色吗?", "删除提示")
        .then(() => {
          deleteRole(roleId).then((res) => {
            if (res.data.code === "000000") {
              this.$message.success("删除角色成功!");
              this.loadRoles();
            } else {
              this.$message.error("删除角色失败~");
            }
          });
        })
        .catch(() => {
          this.$message.info("取消删除角色的操作...");
        });
      deleteRole();
    },
  },
  //注册
  components: {
    RoleCreateOrEdit,
  },
};
</script>

<style lang="scss" scoped>
//表单样式
.el-form {
  margin-left: 130px;
}
//表格与按钮分开
.el-table {
  margin-top: 20px;
}
</style>
